package com.vae.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.vae.search.mapper.SkuSearchMapper;
import com.vae.search.mapper.SpuSearchMapper;
import com.vae.search.model.SkuEs;
import com.vae.search.model.SpuEs;
import com.vae.search.service.SkuSearchService;
import com.vae.search.service.SpuSearchService;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * es 中spu 服务实现类
 */
@Service
public class SpuSearchServiceImpl implements SpuSearchService {

    @Autowired
    private SpuSearchMapper spuSearchMapper;
    @Autowired
    private SkuSearchMapper skuSearchMapper;

    /**
     * 添加单个spu到es
     * @param spuEs
     */
    @Override
    public void add(SpuEs spuEs) {
        //属性转换
        String spuEsAttributeList = spuEs.getAttributeList();
        if(!StringUtils.isEmpty(spuEsAttributeList)){
            spuEs.setAttrMap(JSON.parseObject(spuEsAttributeList, Map.class));
        }
        //2 查出spu下的sku的所有price取出最小值存入spu
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        queryBuilder.withQuery(QueryBuilders.termQuery("spuId", spuEs.getId()));
        Page<SkuEs> skuEsPage = skuSearchMapper.search(queryBuilder.build());
        List<SkuEs> skuEs = skuEsPage.getContent();
        if(skuEs!=null && skuEs.size()>0){
            Integer minPrice = skuEs.get(0).getPrice();
            for (SkuEs sku : skuEs) {
                if(sku.getPrice()<minPrice){
                    minPrice = sku.getPrice();
                }
            }
            spuEs.setMinPrice(minPrice);
        }
        spuSearchMapper.save(spuEs);
    }

    /**
     * 删除spu
     * @param id
     */
    @Override
    public void del(String id) {
        spuSearchMapper.deleteById(id);
    }

    /**
     * 搜索spu
     * @param searchMap
     * @return
     */
    @Override
    public Map<String, Object> search(Map<String, Object> searchMap) {
        //1 构建查询条件
        NativeSearchQuery nativeSearchQuery = this.generateQuery(searchMap);
        //2 执行查询
        Page<SpuEs> spuEsPage = spuSearchMapper.search(nativeSearchQuery);
        //3 封装返回结果
        Map<String,Object> resultMap = new HashMap<String,Object>();
        resultMap.put("list",spuEsPage.getContent());
        resultMap.put("totalElements",spuEsPage.getTotalElements());
        return resultMap;
    }

    //构建es的查询条件
    public NativeSearchQuery generateQuery(Map<String, Object> searchMap){
        //QueryBuilder构建
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();

        //条件判断
        if(searchMap!=null && searchMap.size()>0){
            //关键词
            String keyword =(String)searchMap.get("keyword");
            if(!StringUtils.isEmpty(keyword)){
                //先通过name进行查询
                queryBuilder.withQuery(QueryBuilders.termQuery("name",keyword.toString()));
            }
        }
        return queryBuilder.build();
    }

}
